package 力扣;

public class 最小字母744 {
    public static char nextGreatestLetter(char[] letters, char target) {
      int l=0;int h=letters.length-1;
      int m=0;
      if (letters[letters.length-1]<=target){
          return letters[0];//找不到则返回第一个
      }
      if (letters[0]>target){
          return letters[0];//第一个元素就大于目标
      }
      while (l<=h){
          m=l+(h-l)/2;
          if (letters[m]<=target){
              l=m+1;
          }else {
              h=m-1;
          }
      }

        //解决m的不确定性问题
        if(letters[m]>target){
            return letters[m];
        }else
            return letters[m+1];
    }

    public static void main(String[] args) {
        char []letters = {'c', 'f', 'j'};
        char []letters2 = {'e','e','e','e','e','e', 'n', 'n','n','n'};
        char target='f';
        char c = nextGreatestLetter(letters, target);

        System.out.println(c);
        //System.out.println('c'>'b');
    }
    public static char nextGreatestLetter2(char[] letters, char target) {
        int n = letters.length;
        int l = 0, h = n - 1;
        while (l <= h) {
            int m = l + (h - l) / 2;
            if (letters[m] <= target) {
                l = m + 1;
            } else {
                h = m - 1;
            }
        }
        return l < n ? letters[l] : letters[0];
    }
}
